<template>
  <div class="login-contain">
    <van-form>
      <van-field
        v-model="username"
        name="用户名"
        label="用户名"
        placeholder="用户名"
        :rules="[{ required: true, message: '请填写用户名', trigger: 'onBlur' }]"
      />
      <van-field
        v-model="password"
        type="password"
        name="密码"
        label="密码"
        placeholder="密码"
        :rules="[{ required: true, message: '请填写密码', trigger: 'onBlur' }]"
      />
      <van-field
        v-model="repassword"
        type="password"
        name="确认密码"
        label="确认密码"
        placeholder="确认密码"
        :rules="pawRules"
      />
      <div style="margin: 16px">
        <van-button round block type="info" @click="register">立即注册</van-button>
      </div>
    </van-form>
    <div class="text" @click="$router.push('/login')">已有账号，去登录</div>
  </div>
</template>

<script>
import { register } from "@/api";
export default {
  data() {
    return {
      username: "",
      password: "",
      repassword: "",
      pawRules: [
        { required: true, message: "请填确认密码" },
        {
          // 自定义校验规则
          validator: (value) => {
            return value === this.password;
          },
          message: "密码不一致",
          trigger: "onBlur",
        },
      ],
    };
  },
  methods: {
    async register() {
      let { message } = await register({
        username: this.username,
        password: this.password,
      });
      this.$toast(message);
      this.$router.replace("login");
    },
  },
};
</script>

<style lang="scss" scoped>
.login-contain {
  font-size: 14px;

  .text {
    font-size: 16px;
    color: #b9b5b5;
    text-align: center;
  }
}
</style>
